using System;

namespace KodeIT.Web
{
    internal class InstanceEvents : IFilterEvents
    {
        internal IHttpFilter Instance;

        internal InstanceEvents(IHttpFilter instance)
        {
            Instance = instance;
        }

        #region IFilterEvents Members

        public event EventHandler<RawDataEventArgs> ReadRawData;
        public event EventHandler<PreProcHeadersEventArgs> PreProcHeaders;
        public event EventHandler<UrlMapEventArgs> UrlMap;
        public event EventHandler<AuthenticationEventArgs> Authentication;
        public event EventHandler<AccessDeniedEventArgs> AccessDenied;
        public event EventHandler<AuthCompleteEventArgs> AuthComplete;
        public event EventHandler<SendResponseEventArgs> SendResponse;
        public event EventHandler<RawDataEventArgs> SendRawData;
        public event EventHandler<ContextEventArgs> EndOfRequest;
        public event EventHandler<LogEventArgs> Log;
        public event EventHandler<ContextEventArgs> EndOfNetSession;

        internal virtual void OnReadRawData(RawDataEventArgs e)
        {
            EventHandler<RawDataEventArgs> handler = ReadRawData;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnPreProcHeaders(PreProcHeadersEventArgs e)
        {
            EventHandler<PreProcHeadersEventArgs> handler = PreProcHeaders;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnUrlMap(UrlMapEventArgs e)
        {
            EventHandler<UrlMapEventArgs> handler = UrlMap;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnAuthentication(AuthenticationEventArgs e)
        {
            EventHandler<AuthenticationEventArgs> handler = Authentication;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnAccessDenied(AccessDeniedEventArgs e)
        {
            EventHandler<AccessDeniedEventArgs> handler = AccessDenied;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnAuthComplete(AuthCompleteEventArgs e)
        {
            EventHandler<AuthCompleteEventArgs> handler = AuthComplete;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnSendResponse(SendResponseEventArgs e)
        {
            EventHandler<SendResponseEventArgs> handler = SendResponse;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnSendRawData(RawDataEventArgs e)
        {
            EventHandler<RawDataEventArgs> handler = SendRawData;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnEndOfRequest(ContextEventArgs e)
        {
            EventHandler<ContextEventArgs> handler = EndOfRequest;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnLog(LogEventArgs e)
        {
            EventHandler<LogEventArgs> handler = Log;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        internal virtual void OnEndOfNetSession(ContextEventArgs e)
        {
            EventHandler<ContextEventArgs> handler = EndOfNetSession;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        #endregion

        internal FilterEvent EnumerateEvents()
        {
            FilterEvent events = 0;

            if (null != ReadRawData) { events |= FilterEvent.ReadRawData; }
            if (null != PreProcHeaders) { events |= FilterEvent.PreProcHeaders; }
            if (null != Authentication) { events |= FilterEvent.Authentication; }
            if (null != UrlMap) { events |= FilterEvent.UrlMap; }
            if (null != Authentication) { events |= FilterEvent.Authentication; }
            if (null != AccessDenied) { events |= FilterEvent.AccessDenied; }
            if (null != SendResponse) { events |= FilterEvent.SendResponse; }
            if (null != SendRawData) { events |= FilterEvent.SendRawData; }
            if (null != EndOfRequest) { events |= FilterEvent.EndOfRequest; }
            if (null != Log) { events |= FilterEvent.Log; }
            if (null != EndOfNetSession) { events |= FilterEvent.EndOfNetSession; }

            return events;
        }
    }
}
